package cn.vser.server.demo;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.vser.common.utils.Result;
import cn.vser.server.ApiException;
import cn.vser.server.VserApi;
import cn.vser.server.demo.entity.DataSim;
import cn.vser.server.demo.entity.Room;
import cn.vser.server.entity.AVparam;
import cn.vser.server.entity.Configuration;

@Controller
@RequestMapping("/")
public class DemoController {
    
    /**
     * 用户推流地址
     * @return
     */
	@RequestMapping(value = "stmaddr/push.json")
	@ResponseBody
	public Result<Push> push(
	        @RequestParam(name = "uid") String uid,
	        @RequestParam(name = "uname", required = false) String uname) {
	    Result<Push> result = new Result<>();
	    Configuration conf = null;
	    AVparam avparam = null;
        try {
            conf = VserApi.getInstance().getConfiguration();
            avparam = VserApi.getInstance().getAVparam();
        } catch (ApiException e) {
            result.setErrorCode(e.getErrorCode());
            result.setErrorDesc(e.getErrorDesc());
            return result;
        }
	    Room r = DataSim.getRoomByUid(uid, true, uname);
	    String url = null;
	    try {
            url = VserApi.getInstance().getPushAddr(conf.getPushServer(),
                    r.getScode(), r.getRname(), conf.getAuthSecret(),
                    uid);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSucceed(false);
            return result;
        }
	    Push push = new Push();
	    push.setAddr(url);
		result.setData(push);
		push.setBitrate(avparam.getBitrate());
		push.setFramerate(avparam.getFramerate());
		push.setLandscape(avparam.getLandscape());
		push.setPx(avparam.getPx());
		return result;
	}
	
	/**
	 * 用户播流地址
	 * @return
	 */
	@RequestMapping(value = "stmaddr/pull.json")
    @ResponseBody
    public Result<Pull> pull(@RequestParam(name = "uid") String uid) {
        Result<Pull> result = new Result<>();
        Configuration conf = null;        
        try {
            conf = VserApi.getInstance().getConfiguration();
        } catch (ApiException e) {
            result.setErrorCode(e.getErrorCode());
            result.setErrorDesc(e.getErrorDesc());
            return result;
        }
        Room r = DataSim.getRoomByUid(uid, false, null);
        if (r == null) return result;
        String url = null;
        try {
            url = VserApi.getInstance().getPullAddr(conf.getPullServer(),
                    r.getScode(), conf.getAuthSecret(), uid);
        } catch (Exception e) {
            result.setSucceed(false);
            return result;
        }
        Pull pull = new Pull();
        pull.setAddr(url);
        result.setData(pull);
        return result;
    }
	
    @RequestMapping(value = "room/list.json")
    @ResponseBody
    public Result<List<Room>> list() {
        Result<List<Room>> result = new Result<>();
        try {
            result.setData(DataSim.getRooms());
        } catch (ApiException e) {
            result.setErrorCode(e.getErrorCode());
            result.setErrorDesc(e.getErrorDesc());
            return result;
        }
        return result;
    }
}
